﻿using CIS.Web.Common;
using CIS.Web.Models;
using CIS.Web.Properties;
using System;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace CIS.Web.Controllers
{
	[RoutePrefix("SysConferenceFunctionMaster")]
	public class SysConferenceFunctionMasterController : BaseDbController
	{
		public ActionResult Index()
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName);

			return View();
		}

		public async Task<JsonCamelCaseResult> GetData(int? page, int? rows, string sort, string order, SysConferenceFunctionMasterModel model)
		{
			int take = rows ?? ConstValues.DefaultGridRows;
			int skip = ((page ?? 1) - 1) * take;

			var sql = DbContext.SysFunctionMaster.AsNoTracking()
					.Where(x => !string.IsNullOrEmpty(x.Status) &&
						x.FunctionType == ConstValues.PageLogsType.会议 &&
						x.ID != ConstValues.FunctionId.ConferenceAttendenceForm &&
						x.ID != ConstValues.FunctionId.ConferenceContributeForm);

			if (!string.IsNullOrWhiteSpace(model.SelFunctionName))
			{
				sql = sql.Where(x => x.FunctionName.Contains(model.SelFunctionName) || x.Remark.Contains(model.SelFunctionName));
			}

			int total = await sql.CountAsync();

			if (!string.IsNullOrEmpty(sort))
			{
				sql = sql.OrderBy(sort + ConstValues.chrSpace + order).Skip(skip).Take(take);
			}
			else
			{
				sql = sql.OrderBy(x => x.Sort).ThenByDescending(x => x.UpdateDate).ThenBy(x => x.FunctionName).Skip(skip).Take(take);
			}

			var sqlResult = await sql.ToListAsync();

			var result = sqlResult.Select(x => new
			{
				ID = x.ID,
				FunctionName = x.FunctionName,
				Status = x.Status,
				Sort = x.Sort,
				Remark = x.Remark,
				Creator = x.Creator,
				CreateIP = x.CreateIP,
				CreateDate = LoginUser.Language == ConstValues.Language.Cn ?
							x.CreateDate.Value.ToString("yyyy-MM-dd") :
							x.CreateDate.Value.ToString("MM/dd/yyyy")
			});

			return JsonCamelCase(new
			{
				Total = total,
				Rows = result,
			},
			JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public async Task<JsonResult> Modify(SysConferenceFunctionMasterModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName);

			var result = await DbContext.SysFunctionMaster
					.FirstOrDefaultAsync(x => x.ID == model.Id);

			if (result != null)
			{
				using (var transaction = DbContext.Database.BeginTransaction())
				{
					result.Status = model.Status;
					result.Updater = LoginUser.UserName;
					result.UpdateIP = LoginUser.IP;
					result.UpdateDate = DateTime.Now;

					if (result.ID == ConstValues.FunctionId.ConferenceAttendenceInfo)
					{
						var form = await DbContext.SysFunctionMaster
								.FirstOrDefaultAsync(x => x.ID == ConstValues.FunctionId.ConferenceAttendenceForm);
						form.Status = model.Status;
						form.Updater = LoginUser.UserName;
						form.UpdateIP = LoginUser.IP;
						form.UpdateDate = DateTime.Now;
					}

					if (result.ID == ConstValues.FunctionId.ConferenceContributeInfo)
					{
						var form = await DbContext.SysFunctionMaster
								.FirstOrDefaultAsync(x => x.ID == ConstValues.FunctionId.ConferenceContributeForm);
						form.Status = model.Status;
						form.Updater = LoginUser.UserName;
						form.UpdateIP = LoginUser.IP;
						form.UpdateDate = DateTime.Now;
					}

					await DbContext.SaveChangesAsync();
					transaction.Commit();
				}

				return JsonCamelCase(Utility.CreateSuccessJsonResult(Resources.SuccessSubmit),
						JsonRequestBehavior.AllowGet);
			}
			else
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgRecordNotExist),
						JsonRequestBehavior.AllowGet);
			}
		}
	}
}